// TrackBall.h
//
#ifndef TRACKBALL_ONCE
#define TRACKBALL_ONCE

#include "Math/Math.h"

#include "Framework_Exports.h"

namespace OpenGL {

class FWK_API TrackBall
{
private:
	Math::dvec2 _size;
	//Math::dvec2 _center;
	Math::dvec3 _center;
	Math::dvec3 _eye;
	double      _radius;
	double      _zoom;

	Math::dvec3 _startPoint;
	Math::dquat _prevQ;
	Math::dquat _currQ;
	
	Math::dvec2 normalized( const Math::ivec2 &screenPos ) const;
	Math::dvec3 mapToSphere( const Math::ivec2 &screenPos ) const;

public:
	TrackBall();
	~TrackBall();

	void resize( int pixelWidth,int pixelHeight );
	void setRadius( double radius );
	void setZoom( double zoom );
	void setOrientation( const Math::dmat4 &orientation );
	void setCenter( const Math::dvec3 &center );
	void setEye( const Math::dvec3 &center );
	//void setCenter( const Math::ivec2 &screenPos );
	void reset();

	void startRubberBand( const Math::ivec2 &screenPos );
	void update( const Math::ivec2 &screenPos );
	void endRubberBand( const Math::ivec2 &screenPos );

	Math::dvec3 eye()    const;
	Math::dvec3 center() const;
	Math::dmat4 matrix() const;
	Math::dquat quaternion() const;
	double radius() const;
	double zoom() const;

	void draw() const;
};

}

#endif